<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us" lang="en-us">
<head>
  <link href="//gmpg.org/xfn/11" rel="profile">
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta name="generator" content="Hugo 0.68.3" />

  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>python四大高阶函数（map、reduce、filter、sorted） &middot; 我的博客</title>

  
  <link type="text/css" rel="stylesheet" href="/my_technology_blog/css/print.css" media="print">
  <link type="text/css" rel="stylesheet" href="/my_technology_blog/css/poole.css">
  <link type="text/css" rel="stylesheet" href="/my_technology_blog/css/syntax.css">
  <link type="text/css" rel="stylesheet" href="/my_technology_blog/css/hyde.css">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Abril+Fatface|PT+Sans:400,400i,700">


  
  <link rel="apple-touch-icon-precomposed" sizes="144x144" href="/apple-touch-icon-144-precomposed.png">
  <link rel="shortcut icon" href="/favicon.png">

  
  
</head>

  <body class="theme-base-0b ">
  <aside class="sidebar">
  <div class="container sidebar-sticky">
    <div class="sidebar-about">
      <a href="/my_technology_blog/"><h1>我的博客</h1></a>
      <p class="lead">
       杨博的博客 
      </p>
    </div>

    <nav>
      <ul class="sidebar-nav">
        <li><a href="/my_technology_blog/">Home</a> </li>
        
      </ul>
    </nav>

    <p>&copy; 2021. All rights reserved. </p>
  </div>
</aside>

    <main class="content container">
    <div class="post">
  <h1>python四大高阶函数（map、reduce、filter、sorted）</h1>
  <time datetime=2020-06-26T14:20:45&#43;0800 class="post-date">Fri, Jun 26, 2020</time>
  <h2 id="map">map()</h2>
<blockquote>
<h6 id="map-函数接受两个参数一个是函数一个是序列化map将传入的函数一次作用到序列的每一个元素并把结果作为新的list返回">map() 函数接受两个参数，一个是函数，一个是序列化，map将传入的函数一次作用到序列的每一个元素，并把结果作为新的list返回</h6>
<h6 id="会根据提供的函数对指定的序列做映射">会根据提供的函数对指定的序列做映射</h6>
</blockquote>
<blockquote>
<h4 id="返回值">返回值</h4>
<h6 id="python2是列表">python2：是列表</h6>
<h6 id="python3迭代器">python3：迭代器</h6>
</blockquote>
<p><strong>1、map()用于字典</strong></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">a <span style="color:#f92672">=</span> {<span style="color:#e6db74">&#39;a&#39;</span>: <span style="color:#ae81ff">1</span>, <span style="color:#e6db74">&#39;b&#39;</span>: <span style="color:#ae81ff">2</span>}
la <span style="color:#f92672">=</span> map(str, a)
<span style="color:#66d9ef">print</span>(list(la)) 		<span style="color:#75715e"># [&#39;a&#39;, &#39;b&#39;]</span>
</code></pre></div><p><strong>2、map()用于元组</strong></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">b <span style="color:#f92672">=</span> (<span style="color:#ae81ff">1</span>,<span style="color:#ae81ff">2</span>,<span style="color:#ae81ff">3</span>,<span style="color:#ae81ff">4</span>,<span style="color:#ae81ff">5</span>,<span style="color:#ae81ff">6</span>)
li <span style="color:#f92672">=</span> map(str,b)
<span style="color:#66d9ef">print</span>(list(li)) 		<span style="color:#75715e"># [&#39;1&#39;, &#39;2&#39;, &#39;3&#39;, &#39;4&#39;, &#39;5&#39;, &#39;6&#39;]</span>
</code></pre></div><p><strong>2、map()用于字符串</strong></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-php" data-lang="php">c = &#34;laowang&#34;
lc = map(str,c)
print(list(lc)) 		# [&#39;l&#39;, &#39;a&#39;, &#39;o&#39;, &#39;w&#39;, &#39;a&#39;, &#39;n&#39;, &#39;g&#39;] 
</code></pre></div><h2 id="reduce">reduce()</h2>
<blockquote>
<h5 id="定义">定义</h5>
<h6 id="reduce-函数会对参数序列中的元素进行累积">reduce() 函数会对参数序列中的元素进行累积</h6>
</blockquote>
<p><strong>格式</strong></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">reduce(function, iterable[,initializer]) 
     function:<span style="color:#960050;background-color:#1e0010">函数</span><span style="color:#f92672">---</span><span style="color:#960050;background-color:#1e0010">》有两个参数</span>
     iterable<span style="color:#960050;background-color:#1e0010">：</span><span style="color:#f92672">---</span><span style="color:#960050;background-color:#1e0010">》可迭代对象</span>
     initializer<span style="color:#960050;background-color:#1e0010">：可以选，初始参数</span>
</code></pre></div><hr>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#f92672">from</span> functools <span style="color:#f92672">import</span> reduce
<span style="color:#66d9ef">def</span> <span style="color:#a6e22e">add</span>(a, b):
    <span style="color:#66d9ef">return</span> a <span style="color:#f92672">+</span> b
add(<span style="color:#ae81ff">1</span>, <span style="color:#ae81ff">2</span>)



aa <span style="color:#f92672">=</span> reduce(<span style="color:#66d9ef">lambda</span> x,y:x <span style="color:#f92672">+</span> y, [<span style="color:#ae81ff">1</span>,<span style="color:#ae81ff">2</span>,<span style="color:#ae81ff">3</span>,<span style="color:#ae81ff">4</span>,<span style="color:#ae81ff">5</span>,<span style="color:#ae81ff">6</span>])
<span style="color:#66d9ef">print</span>(aa) 		<span style="color:#75715e"># 21</span>
</code></pre></div><p><strong>获取每一个单词出现的次数</strong></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">str <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;hello python world aa bb cc aa&#34;</span>
list <span style="color:#f92672">=</span> str<span style="color:#f92672">.</span>split(<span style="color:#e6db74">&#34; &#34;</span>)
<span style="color:#66d9ef">def</span> <span style="color:#a6e22e">fun</span>(x,y):
    <span style="color:#66d9ef">if</span> y <span style="color:#f92672">in</span> x:
        x[y] <span style="color:#f92672">=</span> x[y] <span style="color:#f92672">+</span> <span style="color:#ae81ff">1</span>
    <span style="color:#66d9ef">else</span>:
        x[y] <span style="color:#f92672">=</span> <span style="color:#ae81ff">1</span>
    <span style="color:#66d9ef">return</span> x
reslut <span style="color:#f92672">=</span> reduce(fun,list,{})
<span style="color:#66d9ef">print</span>(reslut) 		<span style="color:#75715e"># {&#39;hello&#39;: 1, &#39;python&#39;: 1, &#39;world&#39;: 1, &#39;aa&#39;: 2, &#39;bb&#39;: 1, &#39;cc&#39;: 1}</span>
</code></pre></div><h2 id="filter">filter()</h2>
<blockquote>
<h4 id="定义-1">定义：</h4>
<h6 id="过滤用于过滤序列化过滤掉不符合条件的元素返回由符合条件元素组成的新的列表">过滤，用于过滤序列化，过滤掉不符合条件的元素，返回由符合条件元素组成的新的列表</h6>
</blockquote>
<p><strong>格式</strong></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">filter(function, iterable)
</code></pre></div><hr>
<p><strong>返回值：列表</strong></p>
<p><strong>筛选指定的内容</strong></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">list1 <span style="color:#f92672">=</span> [<span style="color:#ae81ff">1</span>,<span style="color:#ae81ff">2</span>,<span style="color:#ae81ff">3</span>,<span style="color:#ae81ff">4</span>,<span style="color:#ae81ff">5</span>,<span style="color:#ae81ff">6</span>,<span style="color:#ae81ff">7</span>,<span style="color:#ae81ff">8</span>,<span style="color:#ae81ff">9</span>]

<span style="color:#75715e">#筛选条件,偶数保留</span>

<span style="color:#66d9ef">def</span> <span style="color:#a6e22e">func</span>(num):
    <span style="color:#66d9ef">if</span> num <span style="color:#f92672">%</span> <span style="color:#ae81ff">2</span> <span style="color:#f92672">==</span> <span style="color:#ae81ff">0</span>:
        <span style="color:#66d9ef">return</span> True
    <span style="color:#66d9ef">return</span> False

l <span style="color:#f92672">=</span> filter(func,list1)
<span style="color:#66d9ef">print</span>(list(l))
</code></pre></div><div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">data <span style="color:#f92672">=</span> [[<span style="color:#e6db74">&#34;姓名&#34;</span>,<span style="color:#e6db74">&#34;年龄&#34;</span>,<span style="color:#e6db74">&#34;爱好&#34;</span>],[<span style="color:#e6db74">&#34;laowang&#34;</span>,<span style="color:#ae81ff">35</span>,<span style="color:#e6db74">&#34;无&#34;</span>],[<span style="color:#e6db74">&#34;xiaowang&#34;</span>,<span style="color:#ae81ff">18</span>,<span style="color:#e6db74">&#34;金钱&#34;</span>]]
<span style="color:#66d9ef">def</span> <span style="color:#a6e22e">func2</span>(v):
    v <span style="color:#f92672">=</span> str(v)
    <span style="color:#66d9ef">if</span> v <span style="color:#f92672">==</span> <span style="color:#e6db74">&#34;无&#34;</span>:
        <span style="color:#66d9ef">return</span> False

    <span style="color:#66d9ef">return</span> True

<span style="color:#66d9ef">for</span> line <span style="color:#f92672">in</span> data:
    m <span style="color:#f92672">=</span> filter(func2,line)
    <span style="color:#66d9ef">print</span>(list(m))


<span style="color:#75715e"># 结果</span>
[<span style="color:#e6db74">&#39;姓名&#39;</span>, <span style="color:#e6db74">&#39;年龄&#39;</span>, <span style="color:#e6db74">&#39;爱好&#39;</span>]
[<span style="color:#e6db74">&#39;laowang&#39;</span>, <span style="color:#ae81ff">35</span>]
[<span style="color:#e6db74">&#39;xiaowang&#39;</span>, <span style="color:#ae81ff">18</span>, <span style="color:#e6db74">&#39;金钱&#39;</span>]
</code></pre></div><h4 id="heading"></h4>
<h2 id="sorted">sorted</h2>
<blockquote>
<h5 id="定义-2">定义：</h5>
<p>函数对所有的可迭代对象进行排序操作</p>
</blockquote>
<p><strong>sort和sorted的区别</strong></p>
<blockquote>
<h5 id="sort是应用在list上的方法">sort是应用在list上的方法</h5>
<h5 id="sorted所有的可迭代对象进行排序操作">sorted所有的可迭代对象进行排序操作</h5>
</blockquote>
<h5 id="格式">格式</h5>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">sorted(iterable[,cmp[,key[,reverse]]])

iterable:<span style="color:#960050;background-color:#1e0010">可迭代对象</span>
cmp<span style="color:#960050;background-color:#1e0010">：比较函数，具有两个参数，参数的值从可迭代对象中获取，必须遵守规则：</span>
       <span style="color:#960050;background-color:#1e0010">如果大于返回</span><span style="color:#ae81ff">1</span>   <span style="color:#960050;background-color:#1e0010">小于返回</span><span style="color:#f92672">-</span><span style="color:#ae81ff">1</span>   <span style="color:#960050;background-color:#1e0010">等于</span> <span style="color:#960050;background-color:#1e0010">返回</span> <span style="color:#ae81ff">0</span>
    key:<span style="color:#960050;background-color:#1e0010">指定可迭代对象中的一个元素来进行排序</span>
    reverse:<span style="color:#960050;background-color:#1e0010">排序的规则，</span>reverse <span style="color:#f92672">=</span> True  <span style="color:#960050;background-color:#1e0010">降序</span>   <span style="color:#960050;background-color:#1e0010">，</span>reverse <span style="color:#f92672">=</span> False  <span style="color:#960050;background-color:#1e0010">升序</span>   <span style="color:#960050;background-color:#1e0010">默认值</span>
</code></pre></div><p><strong>返回值：重新排完序的列表</strong></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">list <span style="color:#f92672">=</span> [<span style="color:#ae81ff">1</span>,<span style="color:#ae81ff">2</span>,<span style="color:#ae81ff">4</span>,<span style="color:#ae81ff">7</span>,<span style="color:#ae81ff">9</span>,<span style="color:#ae81ff">2</span>,<span style="color:#ae81ff">3</span>,<span style="color:#ae81ff">4</span>,<span style="color:#ae81ff">5</span>]
list1 <span style="color:#f92672">=</span> sorted(list,reverse<span style="color:#f92672">=</span>True)
<span style="color:#66d9ef">print</span>(list1) 		<span style="color:#75715e"># [9, 7, 5, 4, 4, 3, 2, 2, 1]</span>
</code></pre></div><div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">ist <span style="color:#f92672">=</span> [<span style="color:#f92672">-</span><span style="color:#ae81ff">1</span>,<span style="color:#ae81ff">2</span>,<span style="color:#ae81ff">5</span>,<span style="color:#ae81ff">7</span>,<span style="color:#ae81ff">9</span>,<span style="color:#f92672">-</span><span style="color:#ae81ff">2</span>,<span style="color:#f92672">-</span><span style="color:#ae81ff">5</span>]
list1 <span style="color:#f92672">=</span> sorted(list,key<span style="color:#f92672">=</span>abs)
<span style="color:#75715e"># list1 = sorted(list,reverse=True)</span>
<span style="color:#66d9ef">print</span>(list1)		<span style="color:#75715e"># [-1, 2, -2, 5, -5, 7, 9]</span>
</code></pre></div><p><strong>自定义函数</strong></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">list2 <span style="color:#f92672">=</span> [<span style="color:#e6db74">&#34;a&#34;</span>,<span style="color:#e6db74">&#34;aaa&#34;</span>,<span style="color:#e6db74">&#34;bb&#34;</span>,<span style="color:#e6db74">&#34;hello&#34;</span>,<span style="color:#e6db74">&#34;python&#34;</span>]

<span style="color:#66d9ef">def</span> <span style="color:#a6e22e">myLen</span>(str):
    <span style="color:#66d9ef">return</span> len(str)
list3 <span style="color:#f92672">=</span> sorted(list2,key<span style="color:#f92672">=</span>myLen)
<span style="color:#66d9ef">print</span>(list2)    		<span style="color:#75715e"># [&#39;a&#39;, &#39;aaa&#39;, &#39;bb&#39;, &#39;hello&#39;, &#39;python&#39;]</span>
<span style="color:#66d9ef">print</span>(list3)  			<span style="color:#75715e"># [&#39;a&#39;, &#39;bb&#39;, &#39;aaa&#39;, &#39;hello&#39;, &#39;python&#39;]</span>
</code></pre></div>
</div>


    </main>

    
  </body>
</html>
